<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/4/19
 * Time: 10:31 AM
 */

namespace app\common\cachemodel\zl5;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl5ExplainContent extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl5_explain_content';

    protected $cacheModel = 'explain.explain_content';

    public function getExplainContentInfo(int $titleId, string $basis)
    {
        $contentId = $this->getExplainContentId($titleId, $basis);

        return $this->getInfoById($contentId);
    }

    /**
     * 获取 星盘解读词条ID
     * @param int $titleId
     * @param string $basis
     * @return int
     * @throws \Exception
     */
    public function getExplainContentId(int $titleId, string $basis)
    {
        $cacheKey = $titleId . ':' . $basis;
        $contentId = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKey)->setCacheKeyDetail('content_id', $cacheKey)->setCacheType('string')->getDataAndSet(function () use ($titleId, $basis)
        {
            try
            {
                $id = $this->where(['title_id' => $titleId, 'basis' => $basis])->value('id');
            }
            catch (\Exception $e)
            {
                $id = 0;
            }

            return $id;
        }, [], 'content_id');

        return $contentId ? (int)$contentId : 0;
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'type_id,title_id,basis,content,extend,create_time';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $id => $itemData)
        {
            $resData[$id] = $this->dataInit($itemData);
        }

        return $resData;
    }

    public function dataInit(array $data)
    {
        $data = arrayKeyData2int($data, ['id', 'type_id', 'title_id', 'create_time']);

        if (isset($data['extend']))
        {
            $data['extend'] = $data['extend'] ? json_decode($data['extend'], true) : [];
        }

        return $data;
    }

    public function convertBasisToText(string $basis)
    {
        $basisData = explode(':', $basis);
        $basisData2 = explode('-', $basisData[1]);
        $astrologConfig = config('astrolog');

        $basisText = '';
        switch ($basisData[0])
        {
            case 1: // 行星落座     月亮在水瓶座
                $planetsName = explode('|', $astrologConfig['planets_name'][$basisData2[0]])[3];
                $signName = explode('|', $astrologConfig['sign_name'][$basisData2[1]])[3];
                $basisText = "{$planetsName}在{$signName}";
                break;
            case 2: // 行星落宫     木星落12宫
                $planetsName = explode('|', $astrologConfig['planets_name'][$basisData2[0]])[3];
                $basisText = "{$planetsName}落{$basisData2[1]}宫";
                break;
            case 3: // 宫主星飞宫    1宫主飞入12宫
                $basisText = "{$basisData2[0]}宫主飞入{$basisData2[1]}宫";
                break;
            case 4: // 宫头星座     1宫头双鱼座
                $signName = explode('|', $astrologConfig['sign_name'][$basisData2[1]])[3];
                $basisText = "{$basisData2[0]}宫头{$signName}";
                break;
            default:
        }

        return $basisText;
    }
}
